package com.seeyon.apps.extracontract.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.util.Strings;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.cap4.form.bean.FormAuthViewBean;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormBindAuthBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.cap4.form.bean.FormViewBean;
import com.seeyon.cap4.form.bean.SimpleObjectBean;
import com.seeyon.cap4.form.util.Enums;

/**
 * 
 * <p>Title：FormUtils</p>
 * <p>Description：表单工具类</p>
 * <p>Company：seeyon</p>
 * @author zhaoxinyue
 * @version 2021年6月23日
 */
public class FormUtils {
	
	private  FormBean formBean;
	private  FormDataMasterBean bean;
	
	public FormUtils(FormBean formBean, FormDataMasterBean bean) {
		this.formBean = formBean;
		this.bean = bean;
	}
	

	/**
	 * 获取文本类型数据
	 * 
	 * @param display
	 * @return
	 */
	public String getMainTableName(){
		return formBean.getMasterTableBean().getTableName();
	}
	
	/**
	 * 获取文本类型数据
	 * 
	 * @param display
	 * @return
	 */
	public String getValue(String display){
		return bean.getFieldValue(formBean.getFieldBeanByDisplay(display).getName()) != null ? bean.getFieldValue(formBean.getFieldBeanByDisplay(display).getName()).toString(): "";
	}
	
	/**
	 * 获取Long类型数据
	 * 
	 * @param display
	 * @return
	 */
	public long getLongValue(String display){
		return bean.getFieldValue(formBean.getFieldBeanByDisplay(display)
				.getName()) != null ? Long.valueOf(bean.getFieldValue(
						formBean.getFieldBeanByDisplay(display).getName()).toString())
				: 0L;
	}
	
	/**
	 * 获取double类型数据
	 * 
	 * @param display
	 * @return
	 */
	public double getDoubleValue(String display){
		return bean.getFieldValue(formBean.getFieldBeanByDisplay(display)
				.getName()) != null ? Double.valueOf(bean.getFieldValue(
						formBean.getFieldBeanByDisplay(display).getName()).toString())
				: 0.00;
	}
	
	
	/**
	 * 获取int类型数据
	 * 
	 * @param display
	 * @return
	 */
	public Integer getIntValue(String display){
		return bean.getFieldValue(formBean.getFieldBeanByDisplay(display)
				.getName()) != null ? Integer.valueOf(bean.getFieldValue(
						formBean.getFieldBeanByDisplay(display).getName()).toString())
				: 0;
	}
	
	/**
	 * 根据名称获取子表
	 * 
	 * @param display
	 * @return
	 */
	public FormTableBean getSonTableByName(String display){
		FormTableBean sonBean = null;
		List<FormTableBean> sonBeans =formBean.getSubTableBean();
		for (FormTableBean formTableBean : sonBeans) {
			if(formTableBean.getDisplay().equals(display)){
				sonBean = formTableBean;
			}
		}
		return sonBean;
	}
	
	/**
	 * 获取主表字段
	 * 
	 * @param display
	 * @return
	 */
	public String getField(String display){
		return formBean.getFieldBeanByDisplay(display) != null ? formBean.getFieldBeanByDisplay(display).getName() : "";
	}
//	public 
	
	/**
	 * 获取有对应操作权限的 应用绑定 
	 * @param option  add,update....
	 * @return
	 */
	public FormBindAuthBean getBindAuthBean(String option){
		FormBindAuthBean bindAuthBean = null;
		Map<String, FormBindAuthBean> bindAuthBeanMap = formBean.getBind().getUnFlowTemplateMap();
		for (Map.Entry<String, FormBindAuthBean> entry : bindAuthBeanMap.entrySet()) {
			List<SimpleObjectBean> simpleObjectBeans = entry.getValue().getAuthList();
			if (simpleObjectBeans != null && simpleObjectBeans.size() > 0) {
				for (SimpleObjectBean simpleObjectBean : simpleObjectBeans) {
					if (simpleObjectBean.getName().equals(option)) {
						Map<String, Object> extMap = (Map<String, Object>) simpleObjectBean.getJsonObj4Db(true).get("extMap");
						JSONObject postion = JSONObject.parseObject(extMap.get("position").toString());
						boolean toolbar = postion.getBooleanValue("toolbar");
						if (toolbar) {
							if(bindAuthBean==null){
								bindAuthBean = entry.getValue();
							}
						}
					}
				}
			}
		}
		return bindAuthBean;
	}
	
	/**
	 * 获取多视图  的rightId
	 * 
	 * @return
	 */
	public List<String> getRightIdArr(String viewName){
		Map<String, Object> viewMap = new HashMap<String, Object>();
		List<String> rightIds = new ArrayList<String>();
		for (FormViewBean viewBean : formBean.getFormViewList()) {
			if(viewName.contains(viewBean.getFormViewName())){
				if(!viewMap.containsKey(viewBean.getId().toString())){
					viewMap.put(viewBean.getId().toString(), viewBean);
					if(viewBean.isPc()){
						String rightId = "";
						for (FormAuthViewBean auth : viewBean.getAllOperations()) {
							if (!auth.isDelete()) {
								if (Enums.ViewType.SeeyonForm.getText().equals(viewBean.getFormViewType())) {
									if(Strings.isBlank(rightId)){
										rightId = viewBean.getId() + "." + auth.getId();
										rightIds.add(rightId);
									}
								}
							}
						}
					}
				}
			}
        }
		return rightIds;
	}
	


	public FormDataMasterBean getBean() {
		return bean;
	}


	public void setBean(FormDataMasterBean bean) {
		this.bean = bean;
	}
	
}
